(function ($) {
    $.verificationItem = function(t,p){
        var  options = $.extend(true, {

        }, p);
        var reg = $(t).attr("reg");
        if(reg) {
            var regexp = new RegExp(reg);
            if (regexp.test($(t).val())) {
                return true;
            } else {
                $(t).addClass("ui-state-error");
                $(t).focus();
                return false;
            }
        }

    };

    $.fn.verification = function (p) {
        this.removeClass("ui-state-error");
        var flag = true;
        this.each(function (index,item) {
            flag &= $.verificationItem(item,p);
        });
        return flag;

    };

    $.fn.getFlexigridiAttrByIndex=function(findIndex) {
        var descList = [];
        var  selectedDom = $(this).find(".trSelected");
        selectedDom.each(function (index,item) {
            var ch = $(item).attr("ch");
            var atttrs = ch.split("_FG$SP_");
            $(atttrs).each(function (indexCh,itemCh) {
                if(indexCh === findIndex){
                    descList.push(itemCh)
                }
            });

        });
        return descList;
    };

    $.checkItem = function(p){
        console.info(p);
        var pattern = $(p).attr("pattern");
        console.info("pattern:" + pattern);
        if(pattern === null || pattern === ''){
            return false;
        }
        var regext = new RegExp(pattern);
        var val = $(p).val();
        if(regext.test(val)){
            return true;
        }else{
            $(p).addClass("ui-state-error")
            return false;
        }
    }
    $.fn.checkDom = function () {
        this.removeClass("ui-state-error")
        var flag = true;
        this.each(function (index,item) {
            flag &= $.checkItem(item);
        })
        return flag;
    }

    $.fn.searchTools = function(p){
        var  options = $.extend(true, {
            colModel : [
                {display :"", domId:"test-id"},
            ],
            reback:function () {

            }
        }, p);
        var that = $(this);
        that.addClass("search-tools-bar")
        var topIDom = $("<i></i>")
        topIDom.addClass("fa-search").addClass("fa");
        var contentsDiv = $("<div></div>")
        var spanShow = $("<span></span>")
        var inputDom = $("<input type='text'>")
        contentsDiv.append(spanShow);
        contentsDiv.append(inputDom);
        var endIDom = $("<i></i>")
        endIDom.addClass("fa-times").addClass("fa");
        that.append(topIDom);
        that.append(contentsDiv);
        that.append(endIDom);

        var searchUl = $("<ul class='nav search-tools-ul'></ul>")

        $(options.colModel).each(function (index,item) {
            var domLi = $("<li></li>");
            searchUl.append(domLi);
            var domA = $("<a href='#' select-value=''></a>")
            domA.attr("id",item.domId)
            domLi.append(domA)
            var domImgDom = $("<i class='fa fa-check-circle-o'></i>")
            domA.append(domImgDom);
            var domSpanDocm=$("<span></span>")
            domSpanDocm.text(item.display);
            domA.append(domSpanDocm);
            domA.click(function (evnt) {
                evnt.stopPropagation();
                searchUl.css("display","none");
                var inVl = inputDom.val();
                if(inVl !== ''){
                    var selectArray = [];
                    var selectValue =  $(this).attr("select-value");
                    if(selectValue && selectValue !== ''){
                        selectArray = selectValue.split(",");
                    }
                    selectArray.push(inVl);
                    $(this).attr("select-value",selectArray.toString());
                }
                inputDom.val('');
                $.showAttr(searchUl,spanShow);
            });
        });

        $("body").append(searchUl);


        inputDom.mouseover(function (event) {
            event.stopPropagation();
            if( searchUl.hasClass("show-ul-tip")){
                searchUl.css("display","block");
            }
        })

        inputDom.focusin(function (event) {
            event.stopPropagation();
            searchUl.addClass("show-ul-tip");
            //search-tools-bar-focus
            that.addClass("search-tools-bar-focus");
            searchUl.css("display","block");
            searchUl.css("width",that.width() + 1.5 + "px");
        })
        inputDom.focusout(function (event) {
            event.stopPropagation();
            searchUl.removeClass("show-ul-tip")
            that.removeClass("search-tools-bar-focus");
        })

        searchUl.mouseleave(function (event) {
            event.stopPropagation();
            searchUl.css("display","none");
        });
        endIDom.click(function (event) {
            event.stopPropagation();
            var domAlist = searchUl.find("li > a");

            domAlist.each(function (index,item) {
                $(item).attr("select-value","");
                $.showAttr(searchUl,spanShow);

            });
            if(options.reback){
                options.reback();
            }
        })
    }

    $.showAttr = function (searchUl,spanShow) {
        var domAlist = searchUl.find("li > a");
        var spContents = ""
        domAlist.each(function (index,item) {
            var name = $(item).find("span").text();
            var value = $(item).attr("select-value");
            if(value){
                spContents += name + ":" + value + ";"
            }

        });
        spanShow.text(spContents);
    }
})(jQuery);